home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / strlib.zip / GETOPT.C < prev    next >
Text File  |  1993-01-04  |  1KB  |  51 lines

  1.  
  2. /*  File   : getopt.c
  3.     Author : Henry Spencer, University of Toronto
  4.     Updated: 28 April 1984
  5.     Purpose: get option letter from argv.
  6. */
  7.  
  8. #include <stdio.h>
  9. #include "strings.h"
  10.  
  11. char    *optarg;        /* Global argument pointer. */
  12. int     optind = 0;     /* Global argv index. */
  13.  
  14. int getopt(argc, argv, optstring)
  15.     int argc;
  16.     char *argv[];
  17.     char *optstring;
  18.     {
  19.         register int c;
  20.         register char *place;
  21.         static char *scan = NullS;      /* Private scan pointer. */
  22.  
  23.         optarg = NullS;
  24.  
  25.         if (scan == NullS || *scan == '\0') {
  26.             if (optind == 0) optind++;
  27.             if (optind >= argc) return EOF;
  28.             place = argv[optind];
  29.             if (place[0] != '-' || place[1] == '\0') return EOF;
  30.             optind++;
  31.             if (place[1] == '-' && place[2] == '\0') return EOF;
  32.             scan = place+1;
  33.         }
  34.  
  35.         c = *scan++;
  36.         place = index(optstring, c);
  37.         if (place == NullS || c == ':') {
  38.             fprintf(stderr, "%s: unknown option %c\n", argv[0], c);
  39.             return '?';
  40.         }
  41.         if (*++place == ':') {
  42.             if (*scan != '\0') {
  43.                 optarg = scan, scan = NullS;
  44.             } else {
  45.                 optarg = argv[optind], optind++;
  46.             }
  47.         }
  48.         return c;
  49.     }
  50.  
  51.